package ru.reinvent.perspectiva.fragments;

import ru.reinvent.perspectiva.R;

import com.actionbarsherlock.app.SherlockDialogFragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.TextView;

public class AboutFragment extends SherlockDialogFragment 
	{
	protected WebView webView;
	protected boolean isDialog;
	
	public AboutFragment(boolean isDialog)
		{
		this.isDialog = isDialog;
		}
	
	public Dialog onCreateDialog(Bundle savedInstanceState)
		{
		if (!isDialog) return super.onCreateDialog(savedInstanceState);
		
		View content = getActivity().getLayoutInflater().inflate(R.layout.dialog_fragment_title,null);
		((TextView) content.findViewById(R.id.dilog_fragment_title_text)).setText(R.string.menu_about);
		((ViewGroup) content.findViewById(R.id.dilog_fragment_title_content)).addView(webView==null ? createContent() : webView); 
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setView(content);
		
		return builder.create();
		}
	
	@Override
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) 
		{
		if (isDialog) return super.onCreateView(inflater, container, savedInstanceState);
		else if (webView == null) return createContent();
		else return webView;
		}
	
	public boolean isDialog()
		{
		return isDialog;
		}
	
	public void setIsDialog(boolean isDialog)
		{
		this.isDialog = isDialog;
		}
	
	protected WebView createContent()
		{
		webView = new WebView(getActivity());
		webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		webView.loadUrl("file:///android_asset/about/about.html");
		
		return webView;
		}
	}
